﻿// (C) Copyright 2018 by Microsoft 
//
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Windows;

// This line is not mandatory, but improves loading performances
[assembly: ExtensionApplication(typeof(YNCNumberSys.MyPlugin))]

namespace YNCNumberSys
{
    // This class is instantiated by AutoCAD once and kept alive for the 
    // duration of the session. If you don't do any one time initialization 
    // then you should remove this class.
    public class MyPlugin : IExtensionApplication
    {
        public static Autodesk.AutoCAD.Windows.PaletteSet ps_工具条 = null;
        public static AcadToolBarPalette m_toolBar = new AcadToolBarPalette();
        void IExtensionApplication.Initialize()
        {
            // Add one time initialization here
            // One common scenario is to setup a callback function here that 
            // unmanaged code can call. 
            // To do this:
            // 1. Export a function from unmanaged code that takes a function
            //    pointer and stores the passed in value in a global variable.
            // 2. Call this exported function in this function passing delegate.
            // 3. When unmanaged code needs the services of this managed module
            //    you simply call acrxLoadApp() and by the time acrxLoadApp 
            //    returns  global function pointer is initialized to point to
            //    the C# delegate.
            // For more info see: 
            // http://msdn2.microsoft.com/en-US/library/5zwkzwf4(VS.80).aspx
            // http://msdn2.microsoft.com/en-us/library/44ey4b32(VS.80).aspx
            // http://msdn2.microsoft.com/en-US/library/7esfatk4.aspx
            // as well as some of the existing AutoCAD managed apps.

            // Initialize your plug-in application here
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            try
            {
                InitializeMenu();
                初始化收集文档激活事件();
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(Environment.NewLine + ex.ToString());
            }
        }

        void IExtensionApplication.Terminate()
        {
            // Do plug-in application clean up here
        }

         public static void InitializeMenu()
        {
            ////COM方式获取AutoCAD应用程序对象   
            //AcadApplication acadApp = (AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;

            ////为AutoCAD添加一个新的菜单 
            //AcadPopupMenu 菜单 = acadApp.MenuGroups.Item(0).Menus.Add("CAXADN");

            ////声明一个AutoCAD弹出菜单项，用于获取添加的菜单项对象   
            //AcadPopupMenuItem 菜单项;

            ////在新建的菜单中添加一个菜单项 
            //菜单项 = 菜单.AddMenuItem(菜单.Count + 1, "加载工具条", "LOADBAR ");
            //菜单项 = 菜单.AddSeparator(菜单.Count + 1);
            //菜单项 = 菜单.AddMenuItem(菜单.Count + 1, "登录PDM", "LOADPDM ");
            //菜单项 = 菜单.AddMenuItem(菜单.Count + 1, "选择任务", "SELTASK ");
            //菜单项.Enable = false;
            //菜单项 = 菜单.AddMenuItem(菜单.Count + 1, "设置图框", "SETFRAME ");
            //菜单项.Enable = false;
            //菜单项 = 菜单.AddMenuItem(菜单.Count + 1, "管理标题栏", "SETTITLE ");
            //菜单项.Enable = false;
            //菜单项 = 菜单.AddMenuItem(菜单.Count + 1, "管理序号", "SETNO ");
            //菜单项.Enable = false;
            //菜单项 = 菜单.AddMenuItem(菜单.Count + 1, "管理变更标记", "SETCHANGELABEL ");
            //菜单项.Enable = false;
            //菜单项 = 菜单.AddMenuItem(菜单.Count + 1, "管理技术要求", "SETTECH ");
            //菜单项.Enable = false;
            //菜单项 = 菜单.AddMenuItem(菜单.Count + 1, "退出PDM", "EXITPDM ");
            //菜单项.Enable = false;
            //菜单项 = 菜单.AddMenuItem(菜单.Count + 1, "保存", "SAVEPDM ");
            //菜单项.Enable = false;

            //菜单.InsertInMenuBar(acadApp.MenuBar.Count + 1);
            //RunCAXADNMenu();
        }
         /// <summary>
         /// 加载主工具条。
         /// </summary>
         public static void RunYNC_Menu()
         {
             Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
             try
             {
                 if (ps_工具条 == null)
                 {
                     // use constructor with Guid so that we can save/load user data
                     ps_工具条 = new Autodesk.AutoCAD.Windows.PaletteSet("YNC工具栏"); // New Guid("63B8DB5B-10E4-4924-B8A2-A9CF9158E4F6"))
                     ps_工具条.Style = PaletteSetStyles.ShowTabForSingle;
                     ps_工具条.Style = PaletteSetStyles.NameEditable;
                     ps_工具条.Style = PaletteSetStyles.ShowPropertiesMenu;
                     ps_工具条.Style = PaletteSetStyles.ShowAutoHideButton;
                     ps_工具条.Style = PaletteSetStyles.ShowCloseButton;
                     //ps_工具条.Style = PaletteSetStyles.Snappable;
                     ps_工具条.Dock = Autodesk.AutoCAD.Windows.DockSides.None;

                     ps_工具条.Opacity = 90;
                     ps_工具条.Size = new System.Drawing.Size(510, 22);
                     ps_工具条.MinimumSize = new System.Drawing.Size(510, 22);
                     ps_工具条.Dock = Autodesk.AutoCAD.Windows.DockSides.Top;


                     ps_工具条.Dock = Autodesk.AutoCAD.Windows.DockSides.Top;//默认为停靠在上边

                     ps_工具条.DockEnabled = (Autodesk.AutoCAD.Windows.DockSides) //控制面板的拖动范围，只能上和下
                         ((int)Autodesk.AutoCAD.Windows.DockSides.Top + (int)Autodesk.AutoCAD.Windows.DockSides.Bottom);
                     m_toolBar.b_登录 = true;
                     m_toolBar.b_修改密码 = false;
                     m_toolBar.b_用户管理 = false;
                     m_toolBar.b_初始设置 = false;
                     m_toolBar.b_申请图号 = false;
                     m_toolBar.b_图号管理 = false;
                     m_toolBar.b_4C有图申请 = false;
                     m_toolBar.b_4C无图申请 = false;
                     m_toolBar.b_单个文件申请 = false;
                     m_toolBar.b_外协图号申请 = false;
                     m_toolBar.b_图号查询 = false;
                     m_toolBar.b_退出 = false;
                     ps_工具条.Add("YNC工具栏", m_toolBar);
                     ps_工具条.Visible = true;
                     ps_工具条.Size = new System.Drawing.Size(510, 22);
                     ps_工具条.MinimumSize = new System.Drawing.Size(510, 22);
                     ps_工具条.Dock = Autodesk.AutoCAD.Windows.DockSides.Top;
                 }
                 else
                 {
                     ps_工具条.Visible = true;
                 }

             }
             catch
             {
                 ed.WriteMessage("控制面板显示失败");
             }
         }
         #region 初始化收集文档激活事件，通过Com获取新文档激活消息，初始化图层、文字样式、标注样式

         [CommandMethod("初始化收集文档激活事件")]
         public static void 初始化收集文档激活事件()
         {
             // 设注册 DocumentActivated 事件
             Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.DocumentActivated += new DocumentCollectionEventHandler(文档激活处理);
         }

         [CommandMethod("删除收集文档激活事件")]
         public static void 删除收集文档激活事件()
         {
             // 解除注册 COM 事件处理程序
             Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.DocumentActivated -= new DocumentCollectionEventHandler(文档激活处理);
         }

         public static void 文档激活处理(object senderObj, DocumentCollectionEventArgs docColDocActEvtArgs)
         {
             
         }

         #endregion
    }

}
